﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.IO.IsolatedStorage;
using System.IO;
using System.Windows.Media.Imaging;
//using ColourAdvisorPhoneApp.ColourAdvisorServiceReference; // local host service reference
using ColourAdvisorPhoneApp.FashionColourAdvisorCloudServiceReference; // Azure cloud service reference

namespace ColourAdvisorPhoneApp
{
    public partial class ColourTypePage : PhoneApplicationPage
    {
        public ColourTypePage()
        {
            InitializeComponent();
            {
                // Load image of season colour wheel                
                //BitmapImage seasonColourWheel = new BitmapImage(new Uri("/Images;component/Images/SeasonColorWheel.bmp", UriKind.Relative));
                //seasonColourWheelImage.Source = seasonColourWheel;

                //seasonColourWheelImage.Source = new BitmapImage(new Uri("/Images;component/Images/SeasonColorWheel.bmp", UriKind.Relative));
                //Uri uri = new Uri("/Images/SeasonColorWheel.bmp", UriKind.Relative);
                //BitmapImage imgSource = new BitmapImage(uri);
                //seasonColourWheelImage.Source = imgSource;


                // Access App class for user dominant characteristic in isolated storage setting
                DominantCharacteristic result = (Application.Current as App).DominantCharacteristic;
             
                string dominantCharacteristicText = "Your Dominant Colouring is: " + result.ToString();
                dominantTypeTextBlock.Text = dominantCharacteristicText;
               
                // Determine secondary characteristic
                //string question = "Which colours would you say are more flattering on you? Use below colour wheel for guidance";
                string question = "Using the below image as a guide, which colours would you say look best on you?";
                questionTextBlock.Text = question;
                
                List<string> optionA = new List<string>{"Light","Deep"};
                List<string> optionB = new List<string>{"Warm","Cool"};
                
                if (result == DominantCharacteristic.Cool || result == DominantCharacteristic.Warm)
                {
                    
                    questionlistBox.ItemsSource = optionA;
                    // set selected index to 0 so ListBox has a valid SelectedItem at startup
                    questionlistBox.SelectedIndex = 0;
                }
                else
                {
                    questionlistBox.ItemsSource = optionB;
                    // set selected index to 0 so ListBox has a valid SelectedItem at startup
                    questionlistBox.SelectedIndex = 0;
                }                                 
            }
        }

        private void viewResultbutton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // use .SelectedItem on the ListBox to retrieve the selected value and use ToString to return as a string
                string answer = questionlistBox.SelectedItem.ToString();
                // convert string answer to enum type
                SecondaryCharacteristic secondaryCharacteristic = (SecondaryCharacteristic)Enum.Parse(typeof(SecondaryCharacteristic), answer, false);

                // Access App class for user dominant characteristic in isolated storage setting
                DominantCharacteristic dominantCharacteristic = (Application.Current as App).DominantCharacteristic;

                // create proxy
                ColourTypeServiceClient client = new ColourTypeServiceClient();

                // Event handler for when web service call is finished. Event handler calls another method to display the result
                client.DiscoverColourTypeCompleted += new EventHandler<DiscoverColourTypeCompletedEventArgs>(client_DiscoverColourTypeCompleted);
                
                // web service call, pass in user details to method
                client.DiscoverColourTypeAsync(dominantCharacteristic, secondaryCharacteristic);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
        }

        // method to display result of web service call
        void client_DiscoverColourTypeCompleted(object sender, DiscoverColourTypeCompletedEventArgs e)
        {
            // if there are no errors 
            if (e.Error == null)
            {
                try
                {
                    // e.Result is of type enum ColourTypeName
                    // Save this to App class for isolated storage 
                    (Application.Current as App).ColourType = e.Result;

                    // navigate to ColourResultPage where result will be displayed
                    NavigationService.Navigate(new Uri("/ColourResultPage.xaml", UriKind.Relative));
                }
                catch (ArgumentException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                // display error message
                MessageBox.Show(e.Error.ToString());
            }
        }

        private void BackButton_Click(object sender, EventArgs e)
        {
            // Navigate to previous page to select hair colour, eye colour, skin tone
            NavigationService.Navigate(new Uri("/ColouringDetailsPage.xaml", UriKind.Relative));
        }

        private void NextButton_Click(object sender, EventArgs e)
        {
            // Navigate to next page to Colour Type result description
            NavigationService.Navigate(new Uri("/ColourResultPage.xaml", UriKind.Relative));
        }

        private void IntroItem_Click(object sender, EventArgs e)
        {
            // Navigate to Intro page i.e. MainPage
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private void ColourTypeDescriptionItem_Click(object sender, EventArgs e)
        {
            //Navigate to My Colour Type Description page
            NavigationService.Navigate(new Uri("/ColourResultPage.xaml", UriKind.Relative));
        }

        private void ColourTypeImagesItem_Click(object sender, EventArgs e)
        {
            // Navigate to My Colour Type Images page
            NavigationService.Navigate(new Uri("/ColourTypeImagesPage.xaml", UriKind.Relative));
        }

        private void FashionTrendsItem_Click(object sender, EventArgs e)
        {
            // Navigate to Fashion Trends page
            NavigationService.Navigate(new Uri("/FashionTrendsPage.xaml", UriKind.Relative));
        }
    }
}